package com.cskaoyan.shopping.service.impl;

import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlRenameTableStatement;
import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ItemDescConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductCateRequest;
import com.cskaoyan.shopping.dto.AllProductCateResponse;
import com.cskaoyan.shopping.dto.ItemAndDescDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IItemCatService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

/**
 * @author why
 * @since 2022/07/30 15:42
 */
@Service
public class IItemCatServiceImpl implements IItemCatService {

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ItemDescMapper itemDescMapper;

    @Autowired
    ProductCateConverter productCateConverter;

    @Autowired
    ItemDescConverter itemDescConverter;

    @Autowired
    ContentConverter contentConverter;




    @Override
    public AllProductCateResponse selectItemCatAll(AllProductCateRequest request) {
        AllProductCateResponse response = new AllProductCateResponse();

        try {
            request.requestCheck();
            Example example = new Example(ItemCat.class);
            example.setOrderByClause("sort_order ASC");
            List<ItemCat> itemCatList = itemCatMapper.selectByExample(example);
            List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCatList);

            response.setProductCateDtoList(productCateDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }
        return response;
    }

    @Override
    public ProductDetailResponse selectItemAndDesc(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();

        try {
            long id = request.getId();
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
            Item item = itemMapper.selectByPrimaryKey(id);
            ProductDetailDto productDetailDto = itemDescConverter.item2Dto(item);
            productDetailDto.setProductImageBig(item.getImageBig());
            productDetailDto.setProductImageSmall(Arrays.asList(item.getImages()));
            productDetailDto.setDetail(itemDesc.getItemDesc());
            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response,e);
        }

        return response;
    }
}
